<?php

namespace App\Http\Controllers\Api;

use App\Models\Carousel;
use App\Models\Special;
use App\Models\School;
use App\Logic\SchoolLogic;
use App\Logic\ShopGoodsLogic;
use App\Models\ShopTopic;
use App\Http\Resources\ShopTopic as ShopTopicResource;

use Illuminate\Http\Request;


use Alipay\AopClient;
use Alipay\Key\AlipayKeyPair;
use Alipay\Request\AbstractAlipayRequest;
use Alipay\Signer\AlipayRSA2Signer;
use PHPUnit\Framework\TestCase;
//require_once base_path('vendor/alipay/AopSdk.php'); 


class IndexController extends ApiController
{
    /**
     * xiaoT技术首页信息
     * @return mixed
     */
    public function index()
    {
        // 先获取当前登录的用户信息

        if (empty(\Auth::user())) {
            return $this->failed('用户未登录', 401);
        }else{
            $user_id = \Auth::user()->id;
        }
        $outData = [];
        // 专题导航信息
        $outData['specialList'] = Special::getSpecialList();
        
        // 首页轮播
        $outData['carouselInfo'] = Carousel::getCarouselByType(Carousel::BOOTH_TYPE_HOME);
                
        // 热门
        $outData['hotGoodsList'] = ShopGoodsLogic::getGoodsList(['is_hot' => 1], 6);

        // 新品
        $outData['newGoodsList'] = ShopGoodsLogic::getGoodsList(['is_new' => 1], 4);

        // 新品
        $outData['topicList'] = ShopTopicResource::collection(ShopTopic::getTopicListByPage())->additional(['code' => 200]);
        
        return $this->success($outData);
    }
    
    /**
     * 首页三张广告轮播
     * @return mixed
     */
    public function advs(){
        
        $carouselInfo = Carousel::getCarouselByType(Carousel::BOOTH_TYPE_HOME);
        
        return $this->success($carouselInfo);
    }
    
    
    /**
     * 店铺列表
     * @return mixed
     */
    public function store(Request $request){
        
        $where[] = ['status','=',School::IFSHOW_YES];
        
        $shopList =  SchoolLogic::getSchoolList($where);
        
        return $this->success($shopList);
        
    }
    
    /**
     * 首页爆款
     * @return mixed
     */
    public function hotlist(){
        
        $hotGoodsList = ShopGoodsLogic::getGoodsList(['is_hot' => 1], 6);
        
        return $this->success($hotGoodsList);
    }
    
    public function qrcode(){       
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2018103161950699';
        $aop->rsaPrivateKey = '1uxlFJu6C1HATlpawv7aaQ==';
        $aop->alipayrsaPublicKey='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw0B/+HGLXyqDvjwVhnrpCTxKp/gVYe1qC+XUZn/iOd/fKpggkhW8b1AT3SBLwQeivki9pbnfy+bbOCmNo+G/EbZZHK05XK/eUlXsuC1YukjQzL8I2MDZRKr+6FUIX5fT+FqZoHKebCfzrvpkpKBKBu9bJtN6BylXISTr2BVE8vqTp+q9X0xLMTAOCq9KYD7i4qKevgvCiUsH9cuza7bmO5BIccQu4WoWhyhu1QbGIBKzovgylP3chbY2iqXaOEfkPLBWlRLB3NgvvIqTwMjxHJ8VgwMvKDdykDTSk5uQzx0xtjjyXUZ5NUq7xJFqyno4U2bsJwc2VYlkGp2yS/iYIwIDAQAB';
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new \AlipayOpenAppQrcodeCreateRequest ();
        $request->setBizContent("{" .
            "\"url_param\":\"page/component/component-pages/view/view\"," .
            "\"query_param\":\"x=1\"," .
            "\"describe\":\"二维码描述\"" .
            "  }");
        $result = $aop->execute ( $request);
        
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            echo "成功";
        } else {
            echo "失败";
        }
        
    }
}
